// 获取元素的方法
function g(str,obj){
	var o = obj || document;
	var s = str.slice(1,str.length);
	switch(str.substr(0,1)){
		case '#':
			return o.getElementById(s);
		case '.':
			return o.getElementsByClassName(s);
		case '<':
			return o.getElementsByTagName(s);
		case '@':
			return document.getElementsByName(s);
	}

}
// 封装一个ajax方法
function ajax(obj,func,r,ru){
	if(typeof obj != 'object'){// 无效果的数据直接退出不做提示，等于是废弃的操作
		return;
	}
	var u = obj.u;// 定义提交的网址
	var data = obj.data || null;// 定义传送的数据
	var method = obj.method;// 定义传送的方法get|post
	var xhr;
	if(XMLHttpRequest){
		xhr = new XMLHttpRequest();
	}else{
		xhr = new ActiveXObject('Microsoft.XMLHTTP');
	}
	if(xhr != null){
		xhr.onreadystatechange = function(){
			if(xhr.readyState === 4){
				if (func){
					if(r){
						if(ru){
							window[func](xhr.responseText,r,ru);
						}else{
							window[func](xhr.responseText,r);
						}

					}else if(typeof func == "function"){
						func(xhr.responseText);
					}else{
						window[func](xhr.responseText);
					}
				}
			}
		}
		xhr.open(method,u);
		xhr.send(data);
		return false;
	}
}
// 显示方法
function vMsg(t,r,u){
	var vBox = g('#vBox');
	if(t.substr(0,1) == '{' || t.substr(0,1) == '['){
		var msg = eval('('+t+')');
		vBox.style.display = 'block';
		vBox.innerHTML = msg.msg+'<b id="s"></b><br><div id="clsVbox" onclick="vBox.style.display= \'none\'">关闭</div>';
	}else{
		vBox.style.display = 'block';
		vBox.innerHTML = t+'<b id="s"></b><br><div id="clsVbox" onclick="vBox.style.display= \'none\'">关闭</div>';
	}
	vBox.onclick = function(){
		vBox.style.display = 'none';
	}
	vBox.style.display = 'block';
	if(r){
		var s = 3;
		re();
		function re(){
			g('#s').innerHTML = s+'秒后关闭';
			if(--s > 0){
				setTimeout(re,1000);
			}else{
				vBox.style.display = 'none';
				if(u){
					window[r](u);
					console.log(r+'>'+u);
				}else{
					window[r];
					console.log(r);
				}
			}
		}
	}
	
}
function setRem(){
	document.documentElement.style.fontSize = (window.innerWidth/20)+'px';
}
setRem();
function full(){
	var docElm = document.documentElement;
	if (docElm.requestFullscreen) {
		docElm.requestFullscreen();
	}
	//FireFox 
	else if (docElm.mozRequestFullScreen) {
		docElm.mozRequestFullScreen();
	}
	//Chrome等 
	else if (docElm.webkitRequestFullScreen) {
		docElm.webkitRequestFullScreen();
	}
	//IE11 
	else if (docElm.msRequestFullscreen) {
		docElm.msRequestFullscreen();
	}
}

function noFull(){
	if (document.exitFullscreen) {
		document.exitFullscreen();
	}
	else if (document.mozCancelFullScreen) {
		document.mozCancelFullScreen();
	}
	else if (document.webkitCancelFullScreen) {
		document.webkitCancelFullScreen();
	}
	else if (document.msExitFullscreen) {
		document.msExitFullscreen();
	}
}
function carousel(){
	var oCarousel = g('#carousel');
	var oCarLiCount = g('<li',oCarousel).length;
	var oDot = g('#carDot');
	var sC = '';
	for (var i = 0; i < oCarLiCount; i++) {
		sC += '<li></li>';
	}
	oDot.innerHTML = sC;
	var oDotLi = g('<li',oDot);
	oDotLi[0].style.background = '#0CBEA0';
	var p = 0;
	function aLeft(){
		p++;
		if(p >= oCarLiCount){
			p = 0;
		}
		oCarousel.style.left = '-'+(p*20)+'rem';
		for (var i = 0; i < oDotLi.length; i++) {
			oDotLi[i].style.background = '#eee';
		}
		oDotLi[p].style.background = '#0CBEA0';
	}
	function aRight(){
		p--;
		if(p < 0){
			p = oCarLiCount - 1;
		}
		oCarousel.style.left = '-'+(p*20)+'rem';
		for (var i = 0; i < oDotLi.length; i++) {
			oDotLi[i].style.background = '#eee';
		}
		oDotLi[p].style.background = '#0CBEA0';
	}
	var at = setInterval(aLeft,3000);
	var ts,te;
	oCarousel.addEventListener('touchstart',function(e){
		ts = e.touches[0].clientX;
		// console.log(ts);
	});
	oCarousel.addEventListener('touchend',function(e){
		te = e.changedTouches[0].clientX;
		clearInterval(at);
		if(te > ts){
			aRight();
		}else if(te < ts){
			aLeft();
		}
		at = setInterval(aLeft,3000);

	});
}
function topline(){
	var oTl = g('#topline');
	var oTLi = g('<li',oTl);
	var p = 0;
	function ttop(){
		p++;
		if(p >= oTLi.length){
			p = 0;
		}
		oTl.style.top = '-'+(p*2)+'rem';
	}
	var at = setInterval(ttop,3000);
}
function count(){
    var agro = ['抵押住房或商铺贷款','抵押房产及土地贷款','融资性担保公司担保贷款',
				'足额本行存单质押贷款（90%）以下','足额非本行存单质押贷款（90%）以下'];
	var agroLL = [9.00,8.70,7.50,6.00,8.40];
	var opt2 = '';
	for (var i = 0; i<agro.length; i++){
		opt2 += '<option value="'+i+'">'+agro[i]+'</optiona>';
	}
	var oSelect2 = g('#pawn');
	oSelect2.innerHTML = opt2;
	var loanrate = 0;
	var loantotal = 0;
	$(function(){
		$("#bton").click(function(){
			var money = $("#money").val();
			var month = $("#month").val();
			//alert(money+month)
			var s = $("#pawn option:selected").val();
			//alert(s)
			loanrate = agroLL[s];
			$("#rate").val(loanrate);
			loantotal = money*((loanrate/1000)*month+1);
			$("#total").val(loantotal);
		})
	})


	var agrob = ['农户、农业、农副产品加工','工商个体户','小微企业',
				'三个月内流转贷款','小额农贷（余额10万元以内含10万元）'];
	var agroLLb = [11.40,11.76,11.76,15.00,9.60];
	var opt3 = '';
    for (var i = 0; i<agrob.length; i++){
		opt3 += '<option value="'+i+'">'+agrob[i]+'</optiona>';
	}
    var oSelect3 = g('#pawnb');
	oSelect3.innerHTML = opt3;
	var bzrate = 0;
	var bztotal = 0;
	$(function(){
		$("#btonb").click(function(){
			//alert("aaa")
			var money = $("#moneyb").val();
			var month = $("#monthb").val();
			var s = $("#pawnb option:selected").val();
			bzrate = agroLLb[s];
			$("#rateb").val(bzrate);
			bztotal = money*((bzrate/1000)*month+1);
			//alert(bztotal)
			$("#totalb").val(bztotal);
		})
	})


	var aTL = ['活期','三个月','半年','一年','两年','三年','五年'];
	var aLL = [.35,1.43,1.69,1.95,2.73,3.575,4.5];
	var aYL = [0,3,6,12,24,36,60];
	var opt = '';
	for (var i = 0; i<aTL.length; i++){
		opt += '<option value="'+i+'">'+aTL[i]+'</optiona>';
	}
	var oSelect = g('#s');
	oSelect.innerHTML = opt;
	var total = 0;
	var rate = 0;
	var time = 0;
	var interest = 0;
	$(function(){
		g('#cls').onclick = function(){
			g('#ck').value = '';
			g('#lv').value = '';
			g('#lx').value = '';
			g('#sj').value = '';
			g('#bx').value = '';
		};
		g("#button").onclick = function(){
			var ck = $("#ck").val();
			//alert(ck);
			var s = $("#s option:selected").val();
			//alert(s);
				rate=aLL[s];
				$("#lv").val(rate);
			if (s == 0) {
				time = $("#sj").val();
			}else{
				time = aYL[s];
				$("#sj").val(time);
			}
				//alert(time);
				interest = ck*(rate/100)*(time/12);
				//alert(interest);
				$("#lx").val(interest);
				total = ck*((rate/100)*(time/12)+1);
				//alert(total);
				$("#bx").val(total);
		};
	})
}
function clsForm(){
	var oForm = g('<form');
	for (var i = 0; i < oForm.length; i++) {
		oForm[i].style.display = 'none';
		oForm[i].reset();
		g('#signupMsg').innerHTML = '';
		g('#aSignup').className = 'abtn';
	}
}
function fLogin(){ // 登陆方法
	var oLogin = g('#login');
	g('#alogin').onclick = function(){
		oLogin.style.display='block';
	};
	g('#clsLogin').onclick = clsForm;
	g('#aLogin').onclick = fALogin;
	// 用ajax 提交登录表单的方法
	function fALogin(){
		var formdata = new FormData(oLogin);
		var obj = {"method": "post","data": formdata,"u": "http://xtczyh.gscm.tv/api/user/login"};
		ajax(obj,function(res){
			vMsg(res);
			oLogin.style.display='none';
			oLogin.reset();
			var r = eval('('+res+')');
			var user = r.user;
			console.log(user);
		});
	}
}
function fSignup(){ // 注册方法
	var oSignup = g('#signup'); // 注册表单对象
	var aUser_name = g('@user_name');
	var oSmsg = g('#signupMsg');
	var aUser_passwd = g('@user_passwd');// name 为user_passwd 的element数组
	var aUser_repasswd = g('@user_repasswd');// name 为user_repasswd 的element数组
	var pass,repass;
	function getValue(){ //获取 input value的值直接返回
		return this.value;
	}
	g('#aSignup').onclick = fASignup;
	aUser_name[1].onkeyup = function(){
		ajax({"method": "get","u": "http://xtczyh.gscm.tv/api/user/ckUserName?name="+aUser_name[1].value},function(res){
			if(res == '用户名已存在'){
				oSmsg.innerHTML = res;
				g('#aSignup').onclick = null;
				g('#aSignup').className = 'nbtn';
				aUser_passwd[1].disabled = true;
				aUser_repasswd[0].disabled = true;
			}else{
				oSmsg.innerHTML = '';
				g('#aSignup').onclick = fASignup;
				g('#aSignup').className = 'abtn';
				aUser_passwd[1].disabled = false;
				aUser_repasswd[0].disabled = false;
			}
		});
	};
	var upv = getValue.bind(aUser_passwd[1]);// 给密码输入窗加获取值的方法
	var reupv = getValue.bind(aUser_repasswd[0]);// 给验证密码输入窗加获取值的方法
	// 密码输入时的事件
	aUser_passwd[1].onkeyup = function(){
		pass = upv();
		if(aUser_repasswd[0].value != ''){
			if(pass != repass){
				oSmsg.innerHTML = '两次输入的密码不一致';
				g('#aSignup').onclick = null;
				g('#aSignup').className = 'nbtn';
			}else{
				oSmsg.innerHTML = '';
				g('#aSignup').onclick = fASignup;
				g('#aSignup').className = 'abtn';
			}
		}
	};
	// 验证密码输入时的事件
	aUser_repasswd[0].onkeyup = function(){
		repass = reupv();
		if(pass != repass){
			oSmsg.innerHTML = '两次输入的密码不一致';
			g('#aSignup').onclick = null;
			g('#aSignup').className = 'nbtn';
		}else{
			oSmsg.innerHTML = '';
			g('#aSignup').onclick = fASignup;
			g('#aSignup').className = 'abtn';
		}
	}
	// 用户页面注册按钮的点击事件
	g('#asignup').onclick = function(){
		oSignup.style.display='block';
	};
	// 注册表单的注册按钮的点击事件
	g('#clsSignup').onclick = function(){
		clsForm();
		aUser_passwd[1].disabled = false;
		aUser_repasswd[0].disabled = false;
	};
	// 注册表单的取消按钮的点击事件
	// g('#aSignup').onclick = fASignup;
	// 用ajax 提交注册表单的方法
	function fASignup(){
		if (g('@user_name')[1].value == ''){
			oSmsg.innerHTML = '用户名不能为空';
			return;
		}
		var formdata = new FormData(oSignup);
		var obj = {"method": "post","data": formdata,"u": "http://xtczyh.gscm.tv/api/user/Signup"};
		ajax(obj,function(res){
			vMsg(res);
			oSignup.style.display='none';
			oSmsg.innerHTML = '';
			oSignup.reset();
		});
	}
}
window.onresize = setRem;